﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designer.Views
{
    public class RemovePartOperation : OperationBase
    {
        private DesignView _designView;
        private Part _part;
        private int _oldIndex = -1;
        private Designtime.Metadata.NodeBase DT_MetadataForRemoved = null;
        public RemovePartOperation(DesignView designView, Part part)
        {
            _designView = designView;
            _part = part;
        }
        protected override void OnDo()
        {
            Designtime.Metadata.NodeBase nodeBase = null;
            Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, UIContext.Current.CurrentEditedNode.ID);
            if (_part.ParentPart is ScenePart)
            {
                nodeBase = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
            }
            else if (_part.ParentPart is AreaPart)
            {
                Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _part.ParentPart.XFullName);
                if (_part is AreaPart)
                {
                    nodeBase = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
                }
                else
                {
                    nodeBase = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
                }
            }
            else if (_part.ParentPart is Part)
            {
                Designtime.Metadata.Part partParent = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _part.ParentPart.XFullName);
                nodeBase = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
            }
            DT_MetadataForRemoved = nodeBase;
            _oldIndex = (_part.ParentPart as IPartContainer).GetPartIndex(_part);
            _designView.API_Remove(_part);
        }

        protected override void OnUndo()
        {
            _designView.API_AddPart(_part, (IPartContainer)_part.ParentPart, new System.Windows.Point(_part.XLeft, _part.XTop), new System.Windows.Size(_part.XWidth, _part.XHeight), _part.Metadata.Type, DT_MetadataForRemoved);
            if (_oldIndex > -1)
            {
                (_part.ParentPart as IPartContainer).BringPartTo(_part, _oldIndex);
                Designtime.Metadata.Scene scene = Designtime.Metadata.SolutionHelper.GetScene(UIContext.Current.CurrentSolution, UIContext.Current.CurrentEditedNode.ID);
                if (_part.ParentPart is ScenePart)
                {
                    Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
                    scene.BringPartTo(area, _oldIndex);
                }
                else if (_part.ParentPart is AreaPart)
                {
                    Designtime.Metadata.Area area = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _part.ParentPart.XFullName);
                    if (_part is AreaPart)
                    {
                        Designtime.Metadata.Area areaChild = Designtime.Metadata.SolutionHelper.GetArea(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
                        area.BringPartTo(areaChild, _oldIndex);
                    }
                    else
                    {
                        Designtime.Metadata.Part part = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
                        area.BringPartTo(part, _oldIndex);
                    }
                }
                else if (_part.ParentPart is Part)
                {
                    Designtime.Metadata.Part partParent = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _part.ParentPart.XFullName);
                    Designtime.Metadata.Part partChild = Designtime.Metadata.SolutionHelper.GetPart(UIContext.Current.CurrentSolution, scene.Name, _part.XFullName);
                    partParent.BringPartTo(partChild, _oldIndex);
                }
                Event.UI.UIEventBus.Publish(new Event.UI.UIChangePartZIndex(_part.XFullName, _oldIndex));
            }
        }
    }
}
